/**
 * 
 */
package com.feib.stms.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.feib.stms.dao.PreDesignatedAccountDao;
import com.feib.stms.model.AuditEntity;
import com.feib.stms.model.PreDesignatedAccount;
import com.feib.stms.service.PreDesignatedAccountService;


/**
 * @author Karl Chen
 *
 */
@Service("preDesignatedAccountService")
public class PreDesignatedAccountServiceImpl extends BaseServiceImpl implements PreDesignatedAccountService 
{
	private static final long serialVersionUID = 5524105383897392794L;

	@Resource(name="preDesignatedAccountDao")
    private PreDesignatedAccountDao preDesignatedAccountDao;
	
	/**
	 * 根據分公司代號查詢約定帳號
	 * @param securitiesNo
	 * @return
	 */
	public PreDesignatedAccount getPreDesignatedAccountBySecuritiesNo(String securitiesNo, String bankBracnchNo) {
		return preDesignatedAccountDao.getPreDesignatedAccountBySecuritiesNo(securitiesNo, bankBracnchNo);
	}

	/**
	 * 根據分公司代號及群組Pk查詢約定帳號
	 * @param groupPk
	 * @param bankBracnchNo
	 * @return
	 */
	public PreDesignatedAccount getPreDesignatedAccountBySecuritiesNoAndGroupPk(String securitiesNo, Long groupPk, String bankBracnchNo) {
		return preDesignatedAccountDao.getPreDesignatedAccountBySecuritiesNoAndGroupPk(securitiesNo, groupPk, bankBracnchNo);
	}
	
	/**
	 * 新增券商約定帳號
	 * @return
	 */
	public int insert(PreDesignatedAccount preDesignatedAccount){
		return preDesignatedAccountDao.insert(preDesignatedAccount);
	}
	
	/**
	 * 更新券商約定帳號
	 * @param preDesignatedAccountPk 更新前券商約定帳號資料的主鍵 
	 * @param preDesignatedAccount 更新后券商約定帳號資料
	 * @return
	 */
	public int update(Long preDesignatedAccountPk,PreDesignatedAccount preDesignatedAccount){
		//提取更新用戶資料
		AuditEntity auditEntity = new AuditEntity();
		auditEntity.setUpdatedBy(preDesignatedAccount.getUpdatedBy());
		auditEntity.setUpdatedDate(preDesignatedAccount.getUpdatedDate());
		//設定更新舊資料結果
		int updateResult = 0 ;
		//設定更新新資料結果
		int insertResult = 0 ;
		//更新舊的券商約定帳號資料,使之廢棄
		updateResult = preDesignatedAccountDao.update(preDesignatedAccountPk, auditEntity);
		//如果更新成功,則新增最新的約定帳號資料.
		if(updateResult > 0 ){
			insertResult = preDesignatedAccountDao.insert(preDesignatedAccount);
		}		
		return updateResult > 0 && insertResult > 0 ? 1 : 0;
	}
}
